package com.zimmur.service.impl.order;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zimmur.dao.order.IWishListDao;
import com.zimmur.model.fdfs.StorageFiles;
import com.zimmur.model.goods.Goods;
import com.zimmur.model.goods.GoodsColor;
import com.zimmur.model.goods.WishListParam;
import com.zimmur.model.goods.WishListResParam;
import com.zimmur.model.order.WishList;
import com.zimmur.service.fdfs.IStorageFilesService;
import com.zimmur.service.goods.IGoodsService;
import com.zimmur.service.goods.IGoodsSizeService;
import com.zimmur.service.order.ICartService;
import com.zimmur.service.order.IWishListService;
import com.zimmur.tools.DateUtils;
import com.zimmur.tools.PageUtil;

/**
* <p>Title: WishListServiceImpl</p>
* <p>Description: 愿望清单</p>
* <p>Company: zimmur</p>
* <p>Copyright: Copyright (c) 2017</p>
* @author huangsx
* @version 1.0
* @time 2018年1月10日 下午9:27:24
*/
@Service("wishListServiceImpl")
public class WishListServiceImpl implements IWishListService {
	private static Logger log = LoggerFactory.getLogger(WishListServiceImpl.class);
	
	@Resource
	private IWishListDao wishListDao;
	@Resource
	private IGoodsService goodsService;
	@Resource
	private ICartService cartService;
	@Resource  
    private IGoodsSizeService goodsSizeService;
	@Resource
	private IStorageFilesService storageFilesService;
	@Transactional
	@Override
	public boolean addWishList(long goodsId, long userId, String resource) {
		Boolean bRet = false;
		try {
			//1.根据用户idhe查询愿望单是否存在
			WishList wl = new WishList();
			wl.setGoodsId(goodsId);
			wl.setUserId(userId);
			WishList isExist  = wishListDao.selectByGoodsIdAndUserId(wl);
			if(isExist==null){
				//2.添加愿望清单商品信息
				Goods goods = goodsService.selectByGoodsId(goodsId);
				if(goods!=null){
					WishList wishList = new WishList();
					wishList.setCreatedate(DateUtils.getNowUnixTime());
					wishList.setCoverimage(goods.getCoverImage());
					wishList.setGoodsId(goodsId);
					wishList.setUserId(userId);
					wishList.setResource(resource);
					wishList.setTitle(goods.getTitle());
					wishListDao.insertSelective(wishList);
				}
			}
			bRet = true;
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new RuntimeException(e.getMessage());
		}
		return bRet;
	}
	@Transactional
	@Override
	public int MoveInWishList(long userId, long cartId, long goodsId, String resource) {
	    Integer bRet = 0;
		try {
			//1.根据用户id查询愿望单是否存在
			WishList wl = new WishList();
			wl.setGoodsId(goodsId);
			wl.setUserId(userId);
			WishList isExist  = wishListDao.selectByGoodsIdAndUserId(wl);
			if(isExist==null){
				Goods goods = goodsService.selectByGoodsId(goodsId);
				if(goods!=null){
					//2.新增愿望单
					WishList wishList = new WishList();
					wishList.setCreatedate(DateUtils.getNowUnixTime());
					wishList.setCoverimage(goods.getCoverImage());
					wishList.setGoodsId(goodsId);
					wishList.setUserId(userId);
					wishList.setResource(resource);
					wishList.setTitle(goods.getTitle());
					wishListDao.insertSelective(wishList);
					
					//3.删除购物车记录
					 bRet = cartService.delCart(cartId, goodsId);
				}
			}else{//4.愿望单存在直接从购物车删除，不再添加
				bRet = cartService.delCart(cartId, goodsId);
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new RuntimeException(e.getMessage());
		}
		return bRet;
	}		
	@Transactional
	@Override
	public boolean MoveToCart(long userId, long goodsId,long sizeId,Integer quantity,int isPrepay) {
		Boolean bRet = false;
		try {
			//1.校验数据是否存在
			WishList wishList = new WishList();
			wishList.setUserId(userId);
			wishList.setGoodsId(goodsId);
			WishList isExist = wishListDao.selectByGoodsIdAndUserId(wishList);
			if(isExist !=null){
				//2.新增购物车
				cartService.addCart(userId,goodsId, sizeId, quantity,isPrepay);
				//3.删除愿望清单中的商品
				wishListDao.deleteByGoodsIdWithUserId(wishList);
				bRet = true;
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return bRet;
	}

	@Override
	public Boolean delete(long userId,long goodsId) {
		Boolean bRet = false;
		try {
			//1.校验数据是否存在
			WishList wishList = new WishList();
			wishList.setUserId(userId);
			wishList.setGoodsId(goodsId);
			WishList isExist = wishListDao.selectByGoodsIdAndUserId(wishList);
			if(isExist !=null){
				//2.删除
				wishListDao.deleteByGoodsIdWithUserId(wishList);
				bRet = true;
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return bRet;
	}
	@Override
	public WishList selectByGoodsIdAndUserId(long userId, long goodsId) {
		WishList wl = new WishList();
		wl.setGoodsId(goodsId);
		wl.setUserId(userId);
		return wishListDao.selectByGoodsIdAndUserId(wl);
	}
	@Override
	public PageUtil<WishListResParam> queryByUser(WishListParam param) {
		PageUtil<WishListResParam> page = new PageUtil<WishListResParam>();
		int count = wishListDao.countByParam(param);
		List<WishListResParam> list = wishListDao.readByMuchParams(param);
		if(list!=null&&!list.isEmpty()){
			for(WishListResParam p:list){
				//1.添加颜色尺码
				List<GoodsColor> goodsColorList =goodsSizeService.selectGoodsColorByGoodsId(p.getGoodsId());
				p.setGoodsColorList(goodsColorList);
				//添加图片的Url
				StorageFiles storageFiles = storageFilesService.selectByFileId(Long.parseLong(p.getCoverimage()));
				if(storageFiles!=null){
					p.setCoverimageUrl("/"+storageFiles.getStoragegroup()+"/"+storageFiles.getStorageid());
				}
			}
		}
		page.setTotalRecord(count);
		page.setPageNo(param.getPageIndex());
		page.setPageSize(param.getPageSize());
		page.setItemList(list);
		return page;
	}
	@Override
	public List<WishList> selectByUserId(long userId) {
		return wishListDao.selectByUserId(userId);
	}
	@Override
	public int queryWishNumByUserId(long userId) {
		List<WishList> wishLists = selectByUserId(userId);
		return wishLists.size();
	}

	
}
